/*
 * Function: Token array's processor
 */
package main;

import java.util.ArrayList;
import java.util.Collections;

public class TokenArrayProcessor 
{
	private ArrayList<tokens> arlTokensArray;
	private int intIndex;
	
	/*
	 * Function: Initial function method
	 * Input: pTokenArray -> TokenArray
	 * Output: None
	 */
	public TokenArrayProcessor(ArrayList<tokens> pTokenArray)
	{
		arlTokensArray = pTokenArray;
		intIndex = 0;
	}
	
	public void initialIndex()
	{
		intIndex = 0;
	}
	
	public void setTokens(ArrayList<String> pTokens, int pLine)
	{
		String strLine = Integer.toString(pLine);
		
		Collections.sort(pTokens);
		
		//*********************************************8
		//System.out.println("Line " + strLine + " contains " + pTokens.size() + " tokens");
		//*********************************************8
		
		for (int i = 0; i < pTokens.size(); i++)
		{
			insertToken(pTokens.get(i), strLine);
		}	
	}
	
	private void insertToken(String pToken, String pLine)
	{
		//**********************************************
		/*System.out.print("Current Array: ");
		printArray();		
		System.out.println();
		
		System.out.println("Current Line: " + pLine +" Current Token: " + pToken);
		System.out.println("Current Index: " + intIndex +" Current Size: " + arlTokensArray.size());*/
		//**************************************************
		
		if (intIndex == arlTokensArray.size())
		{
			arlTokensArray.add(new tokens(pToken, pLine));
			//*********************************************
			/*System.out.println("first token in the Array");*/
			//**********************************************
		}
		else
		{
			if (arlTokensArray.get(intIndex).getToken().equals(pToken))
			{
				arlTokensArray.get(intIndex).addLocation(pLine);
				intIndex ++;
				//********************************************
				/*System.out.println("already existed");*/
				//********************************************
			}
			else if(arlTokensArray.get(intIndex).getToken().compareToIgnoreCase(pToken) > 0)
			{
				//*************************************************
				/*System.out.println("smaller than current index");
				System.out.print("Array before insert: ");
				printArray();*/
				//*************************************************
				
				arlTokensArray.add(arlTokensArray.get(arlTokensArray.size() - 1));
				for(int i = arlTokensArray.size() - 3; i >= intIndex  ; i--)
				{
					arlTokensArray.set(i + 1, arlTokensArray.get(i));
				}
				arlTokensArray.set(intIndex, new tokens(pToken, pLine));
				//*************************************************
				/*System.out.print("Array after insert: ");
				printArray();*/	
				//*************************************************
			}
			else
			loop: {
				while (arlTokensArray.get(intIndex).getToken().compareToIgnoreCase(pToken) < 0)
				{
					intIndex ++;
					//**************************************************
					/*System.out.println("bigger than current index");
					System.out.println("Current Index: " + intIndex +" Current Size: " + arlTokensArray.size());*/
					//***************************************************
					
					if (intIndex == arlTokensArray.size())
					{
						arlTokensArray.add(new tokens(pToken, pLine));
						//System.out.println("added to the end of Array");
						break loop;
					}					
				}		
				
				if (arlTokensArray.get(intIndex).getToken().equals(pToken))
				{
					arlTokensArray.get(intIndex).addLocation(pLine);
					intIndex ++;
					//*********************************************
					/*System.out.println("already existed");*/
					//***********************************************
				}
				else
				{
					//*************************************************
					/*System.out.println("smaller than current index");
					System.out.print("Array before insert: ");
					printArray();*/
					//****************************************************
					arlTokensArray.add(arlTokensArray.get(arlTokensArray.size() - 1));
					for(int i = arlTokensArray.size() - 3; i >= intIndex  ; i--)
					{
						arlTokensArray.set(i + 1, arlTokensArray.get(i));
					}
					arlTokensArray.set(intIndex, new tokens(pToken, pLine));
					//******************************************************
					/*System.out.print("Array after insert: ");
					printArray();*/
					//******************************************************
				}
			}
		}
		
		
	}
	
	public ArrayList<tokens> getTokensArray()
	{
		return arlTokensArray;
	}
	
	//*********************************************
	/*private void printArray()
	{
		for (int i = 0; i < arlTokensArray.size(); i++)
		{
			System.out.print(arlTokensArray.get(i).getToken() + ", ");
		}
		System.out.println();
	}*/
}
